
    <div class="content margin_top">
        <header class="header fixed_top bg_mx_green"></header>
        <form class="farmerinfo"></form>
    </div>
    <div class="all_swipe swipe hide">
        <div class='swipe-wrap'></div>
        <ul class="position"></ul>
        <div class="swipe_close">&times;</div>
    </div>
    <script type="text/template" charset="utf-8" id='swipe_template'></script>
    <div class="toast"></div>
    <!-- toast模版 -->
    <script type="text/template" charset="utf-8" id='toast_template'>
        <i class="iconfont icon-toast{{=it.icon}}"></i>
        <div class="toast_text">{{=it.text}}</div>
    </script>
    <script type="text/template" charset="utf-8" id='info_template'>
        <div class="bg_white margin_b_10">
            <div class="flex border_b padding_10_15 border_b">
                <div class="width_70">基本情况</div>
            </div>
            <div class="padding_h_15 padding_v_5 border_b lineheight_3">
                <div class="flex">
                    <div class="width_70">姓名：</div>
                    <div class="left_70">
                        <input type="text" name="name" value="{{=it.name||''}}" placeholder="请填入姓名" request title="姓名">
                        <input type="hidden" name="farmerid" value="{{=it.farmerid||''}}">
                    </div>
                </div>
                <div class="flex">
                    <div class="width_70">电话：</div>
                    <div class="left_70">
                        <input type="text" name="phone" value="{{=it.phone||''}}" placeholder="请填入电话号码" request title="电话" data-pattern="^(0|86|17951)?(13\d|15[0-35-9]|17[678]|18\d|14[57])\d{8}$" data-tips="11位手机号">
                    </div>
                </div>
                <div class="flex">
                    <div class="width_70">身份证号：</div>
                    <div class="left_70">
                        <input type="text" name="identity" value="{{=it.identity||''}}" placeholder="请填入身份证号" request title="身份证号" data-pattern="^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$" data-tips="正确身份证号">
                    </div>
                </div>
                <div class="flex">
                    <div class="width_70">地址：</div>
                    <div class="left_70 flex">
                        <div class="flex_1">
                            <select class="select1" name="province" request title="请填写县" id="">
                                <option value="">县</option>
                            </select>
                        </div>
                        <div class="flex_1">
                            <select class="select2" name="city"  request title="请填写乡/镇" id="">
                                <option value="">乡/镇</option>
                            </select>
                        </div>
                        <div class="flex_1">
                            <select class="select3" name="area" request title="请填写村" id="">
                                <option value="">村</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="flex">
                    <div class="width_70">性别：</div>
                    <div class="left_70 flex">
                        <div class="flex_1">
                            <select name="gender" id="gender">
                                <option value="男" {{if(it.gender=='男'){}}selected{{}}}>男</option>
                                <option value="女" {{if(it.gender=='女'){}}selected{{}}}>女</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="flex">
                    <div class="width_70">详细地址：</div>
                    <div class="left_70">
                        <textarea class="padding_v_5" name="address" rows="2" placeholder="请输入详细地址" request title="详细地址">{{=it.address||''}}</textarea>
                    </div>
                </div>
                <div class="flex">
                    <div class="width_70">猪舍简介：</div>
                    <div class="left_70">
                        <textarea class="padding_v_5" name="pighousedesc" rows="3" placeholder="农户猪舍简介" title="农户猪舍简介">{{=it.pighousedesc||''}}</textarea>
                    </div>
                </div>
                <div class="flex">
                    <div class="width_70">农户简介：</div>
                    <div class="left_70">
                        <textarea class="padding_v_5" name="descripation" rows="3" placeholder="请填入农户简介" title="农户简介">{{=it.descripation||''}}</textarea>
                    </div>
                </div>
            </div>
        </div>
        <div class="flex border_b padding_10_15 border_b bg_white">
            <div class="width_70">图片</div>
        </div>
        <div class="padding_15 bg_white margin_b_10">
            <div class="preview2 flex_row_3 margin_b_10"></div>
            <div class="light lineheight_3 v_center input2">
                <div class="width_50 border text_center relative">
                    <div class="iconfont icon-add font_20"></div>
                    <input class="absolute all" type="file" value="" accept="image/*" style="opacity:0;" onchange="selectpic(this,2)"/>
                    <input type="hidden" name="photo" value=""/>
                </div>
                <div class="left_50  padding_l_15">点击上传农户图片</div>
            </div>
        </div>
        <div class="padding_15 bg_white margin_b_10">
            <div class="preview0 flex_row_3 margin_b_10"></div>
            <div class="light lineheight_3 v_center input0">
                <div class="width_50 border text_center relative">
                    <div class="iconfont icon-add font_20"></div>
                    <input class="absolute all" type="file" value="" accept="image/*" style="opacity:0;" onchange="selectpic(this,0)"/>
                    <input type="hidden" name="infoimg" value=""/>
                </div>
                <div class="left_50  padding_l_15">点击上传身份图片</div>
            </div>
        </div>
        <div class="padding_15 bg_white margin_b_10">
            <div class="preview1 flex_row_3 margin_b_10"></div>
            <div class="light lineheight_3 v_center input1">
                <div class="width_50 border text_center relative">
                    <div class="iconfont icon-add font_20"></div>
                    <input class="absolute all" type="file" value="" accept="image/*" style="opacity:0;" onchange="selectpic(this,1)"/>
                    <input type="hidden" name="houseimg" value=""/>
                </div>
                <div class="left_50  padding_l_15">点击上传猪舍图片</div>
            </div>
        </div>
        <div class="padding_15 font_16">
            <button class="btn btn_mx_green all_width lineheight_3">保存</button>
        </div>
        <input name="_csrf" type="hidden" id="_csrf" value="<{$app->request->csrfToken}>">
        <input name="farmerid" type="hidden" id="farmerid" value="<{$app->request->get('farmerid')}>">
    </script>
    <script type="text/template" charset="utf-8" id='area_template'>
        {{if(Object.keys(it.data).length){}}
            {{for(var i in it.data){ }}
                <option value="{{=i}}"{{ if(it.val==i){ }}selected{{ } }}>{{=it.data[i].name}}</option>
            {{ } }}
        {{}else{}}
            <option value="">暂无</option>
        {{}}}
    </script>
    <script type="text/template" charset="utf-8" id='img_template'></script>
    <script type="text/template" charset="utf-8" id='img_one_template'>
        <div class="row_one relative">
            <div class="del_img" onclick="del({{=it.n}},{{=it.i}})">&times;</div>
            <img class="fit_img2" onclick="preview({{=it.n}},{{=it.i}})" src="{{=it.d}}">
        </div>
    </script>
    <script>
        // 页面初始化
            var farmer = <{$farmer}>;console.log(farmer)
            var evalinfo = doT.template($("#info_template").text());
            var evalToast = doT.template($("#toast_template").text());
            loaddata(".farmerinfo",evalinfo,farmer);
            loadheadbar("资料维护",1);
        // 城市联动数据初始化
            var address = <{$address}>;console.log(address)
        console.log(address);
            var evalarea = doT.template($("#area_template").text())
            level({ele:".select",data:address,level1:parseInt(farmer.province)||'',level2:parseInt(farmer.city)||'',level3:parseInt(farmer.area)||'',func:evalarea})
        // 选择图片
            var img_old=[farmer.infoimg||[],farmer.houseimg||[],farmer.personalimg||[]],img_arr=[[],[],[]],imgs=[[],[],[]],evalImg,evalimg_one = doT.template($("#img_one_template").text());console.log(img_old);
            loadimg(function(){
                evalImg = doT.template($("#img_template").text());
                for (var i = 0; i < img_old.length; i++) {
                    $(".preview"+i).html(evalImg({n:i,d:img_old[i],already:1}))
                    if($(".preview"+i).find(".row_one").length >= 3){
                        $(".input"+i).hide();
                    }
                }
            })
            function selectpic(obj,i){
                var file = obj.files[0];
                var URL = window.URL || window.webkitURL;
                var preview = URL.createObjectURL(file);
                tobase64(file,function(res){
                    imgs[i].push(res);
                })
                img_arr[i].push(preview);
                $(obj).val("");
                $(".preview"+i).append(evalimg_one({n:i,i:img_arr[i].length-1,d:preview}))
                if($(".preview"+i).find(".row_one").length >= 3){
                    $(".input"+i).hide();
                }
            }
        // 删除图片
            function del(n,i,already){
                $(".input"+n).show();
                if(already){
                    var type = (n==0)?"identity_photo":(n==1)?"pighousephoto":"photo";
                    $.post("/business/person/delete-farmer-img",{type:type,farmerid:farmer.farmerid,key:i,_csrf:'<{$app->request->csrfToken}>'},function(res){
                        toast("attention",res.msg)
                        if(!res.status){
                            img_old[n].splice(i,1);
                            init_img(n)
                        }
                    },"json")
                }else{
                    imgs[n].splice(i,1);
                    img_arr[n].splice(i,1);
                    init_img(n)
                }
            }
            function init_img(n){
                $(".preview"+n).html("")
                if (img_old[n]) {
                    $(".preview"+n).html(evalImg({n:n,d:img_old[n],already:1}))
                }
                if (img_arr[n]) {
                    $(".preview"+n).append(evalImg({n:n,d:img_arr[n]}))
                }
            }
        // 提交表单
            var submited = false;
            $(".farmerinfo").submit(function(e){
                e.preventDefault();
                with(this){
                    infoimg.value = imgs[0].join("%^");
                    houseimg.value = imgs[1].join("%^");
                    photo.value = imgs[2].join("%^");
                    for (var i = 0; i < this.length; i++) {
                        if($(this[i]).attr("request")==""){
                            if(!checkone(this[i])){
                                return false;
                            }
                        }
                    }
                    if(!$(".preview2").find(".row_one").length){
                        toast("attention","请上传农户图片");
                        return false;
                    }
                    if(!$(".preview0").find(".row_one").length){
                        toast("attention","请上传身份图片");
                        return false;
                    }
                    if(!$(".preview1").find(".row_one").length){
                        toast("attention","请上传猪舍图片");
                        return false;
                    }
                    if(submited) return false;
                    var data = $(".farmerinfo").serialize();
                    $.post("/business/person/post-farmer-info",data,function(res){
                        toast("attention",res.msg);
                        submited = true;
                        if(res.status == 0){
                            location.href='/business/person/farmer'
                        }
                    },"json")
                }
            })
        // 预览轮播
            function preview(n,i,already){
                if(!already){
                    i = img_old[n].length + i;
                }
                var pre_imgs = img_old[n].concat(imgs[n]);
                $(".swipe").show();
                $("body").scrollTop(0).addClass("row")
                $(window).on('touchmove', function (e) {
                    e.preventDefault();
                });
                loadswipe(pre_imgs,i,function(){
                    var h = ($(window).height() - $(".swipe_e:first-child").height())/2;
                    $(".swipe_e").css("margin-top",h+"px")
                })
            }
            $(".swipe_close").click(function(){
                $(".swipe").hide()
                $("body").removeClass("row")
                $(window).unbind('touchmove');
            })
    </script>
